package com.guhappy.model.user;

// Generated Jul 19, 2011 6:12:32 PM by Hibernate Tools 3.4.0.CR1

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.guhappy.util.HibernateListener;

/**
 * Home object for domain model class Customers.
 * @see  bd
 * @author  B.D
 */
public class CustomerHome {

	private static final Logger log = Logger.getLogger(CustomerHome.class);

	private static final SessionFactory sessionFactory = getSessionFactory();

	protected static SessionFactory getSessionFactory() {
		SessionFactory sessionFactory = 
	         (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);
		return sessionFactory;
	}

	public static void persist(Customer transientInstance) {
		log.debug("persisting Customers instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public static void attachDirty(Customer instance) {
		log.debug("attaching dirty Customers instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(Customer instance) {
		log.debug("attaching clean Customers instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(Customer persistentInstance) {
		log.debug("deleting Customers instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static Customer merge(Customer detachedInstance) {
		log.debug("merging Customers instance");
		try {
			Customer result = (Customer) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static Customer findById(java.lang.String id) {
		log.debug("getting Customers instance with id: " + id);
		Session session=null;
		Customer customer=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.user.Customer c where c.id = :id");
			q.setString("id", id);
			customer=(Customer) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return customer;
	}
}
